package danling

import (
	"bytes"
	"crypto/des"
	"gitee.com/thingple/detonator/utils"
	"time"
)

func now() string {
	return time.Now().Format(time.DateTime)
}

func encode(originalBytes []byte, key []byte) (encoded []byte, err error) {

	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		return
	}
	blockSize := block.BlockSize()

	paddingBytes := PKCS5Padding(originalBytes, blockSize)
	blockMode := utils.NewECBEncrypter(block)

	cipherBytes := make([]byte, len(paddingBytes))
	blockMode.CryptBlocks(cipherBytes, paddingBytes)
	encoded = cipherBytes
	return
}

func PKCS5Padding(data []byte, blockSize int) []byte {
	padding := blockSize - len(data)%blockSize // 要填充的值和个数
	slice1 := []byte{byte(padding)}            // 要填充的单个二进制值
	slice2 := bytes.Repeat(slice1, padding)    // 要填充的二进制数组
	return append(data, slice2...)             // 填充到数据末端
}
